/**
 * 
 */
package fr.cephb.joperon.core;

import java.util.List;



import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.DatabaseException;


import fr.cephb.joperon.core.bio.Chromosome;
import fr.cephb.joperon.core.bio.Organism;


/**
 * an specialisation of {@link  OperonDBDuplicateValue} having a {@link Fid} as a key
 * @param <V> the value
 */
public abstract class OperonDBDupFid<V> extends OperonDBDuplicateValue<Fid,V> {

	/**
	 * Constructor
	 * @param operon the operon instance
	 */
	public OperonDBDupFid(Operon operon) {
		super(operon);
	}


	@Override
	public Fid keyFromString(String s)
		{
		return new Fid(s);
		}


	@Override
	protected DatabaseEntry keyToEntry(Fid key) throws DatabaseException {
		return new DatabaseEntry(key.as0EndedByteArray());
	}


	@Override
	public Fid readKey(DatabaseEntry key, DatabaseEntry data)
			throws DatabaseException {
		return new Fid(key);
	}

	/**
	 * get The Organism for this database
	 * @return
	 */
	public abstract Organism getOrganism();
	
	
	/**
	 * list all the values for a given chromosome, for the given range
	 * @param chr chromosome
	 * @param start start position
	 * @param end end position
	 * @return a ValueIterator over the values
	 * @throws DatabaseException
	 */
	public ValueIterator listBetween(Chromosome chr,int start,int end )  throws DatabaseException
		{
		FidEntriesIterator iter= new FidEntriesIterator(getOrganism(),chr,start,end);
		return new ValueIterator(iter);
		}
	
	/**
	 * list all the values for a given chromosome
	 * @param chr chromosome
	 * @return a ValueIterator over the values
	 * @throws DatabaseException
	 */
	public ValueIterator list(Chromosome chr)  throws DatabaseException
		{
		return listBetween(chr,0,Fid.MAX_VALUE);
		}
	
	/**
	 * get all the values for a given chromosome
	 * @param chr chromosome
	 * @return  all the values
	 * @throws DatabaseException
	 */
	public List<V> get(Chromosome chr)  throws DatabaseException
		{
		return list(chr).asList();
		}

}
